package com.th.supcom.pds.impl.service.adapter;

import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import net.carefx.framework.exception.BusinessException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.th.supcom.framework.commoms.uitls.FcAgeUtil;
import com.th.supcom.pds.domain.vo.PatientInfo;
import com.th.supcom.pds.impl.common.enums.PrescSource;
import com.th.supcom.pds.impl.common.utils.ExceptionThrowHelper;
import com.th.supcom.pds.medicare.api.AdapterPtsExtOutPatientVisitService;
import com.th.supcom.pts.external.patient.api.query.in.PtsExtInPatientVisitQueryService;
import com.th.supcom.pts.external.patient.api.query.out.PtsExtOutPatientVisitQueryService;
import com.th.supcom.pts.external.patient.domain.in.PtsExtInPatientVisitInfo;
import com.th.supcom.pts.external.patient.domain.out.PtsExtOutPatientVisitInfo;

public class AdapterPtsExtOutPatientVisitServiceImpl implements AdapterPtsExtOutPatientVisitService
{
    private PtsExtOutPatientVisitQueryService m_ptsExtOutPatientVisitService;
    private PtsExtInPatientVisitQueryService m_ptsExtInPatientVisitQueryService;

    public void setPtsExtOutPatientVisitService (PtsExtOutPatientVisitQueryService ptsExtOutPatientVisitService)
    {
        m_ptsExtOutPatientVisitService = ptsExtOutPatientVisitService;
    }

    public void setPtsExtInPatientVisitQueryService (PtsExtInPatientVisitQueryService ptsExtInPatientVisitQueryService)
    {

        m_ptsExtInPatientVisitQueryService = ptsExtInPatientVisitQueryService;
    }

    /**
     * 日志记录器.
     */
    private static final Logger s_logger = LoggerFactory.getLogger (AdapterPtsExtOutPatientVisitServiceImpl.class);
    /**
     * 公司版权信息.
     */
    public static final String copyright = "Copyright 2014, Carefx Corporation";

    /**
     * 提交路径.
     */
    public static final String SCC_BRANCH = "$URL$";

    /**
     * 提交日期.
     */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /**
     * 最终修改人员.
     */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /**
     * 最终版本号.
     */
    public static final String SCC_REVISION = "$Revision$";

    /**
     * 
     * @return add by Zli
     */
    @Override
    public PatientInfo getPatientInfo (Long patientVisitId, Integer prescSource, String patientId)
    {
        s_logger.debug ("调用getPatientInfo方法,入参:patientVisitId:{},prescSource:{},patientId:{}", new Object[]
        { patientVisitId, prescSource, patientId });
        ExceptionThrowHelper.throwIllegalArgumentException (prescSource == null, "患者信息查询出现异常:处方来源为空！");
        PatientInfo patientInfo = new PatientInfo ();
        patientInfo.setPrescSource (prescSource);// 处方来源
        PtsExtOutPatientVisitInfo outPatientVisitInfo = new PtsExtOutPatientVisitInfo ();
        outPatientVisitInfo.setOutpVisitId (patientVisitId);
        outPatientVisitInfo.setPatientId (patientId);
        // 如果处方来源门诊 调PTS得到患者基本信息
        if (!isHospital (prescSource))
        {
            List <PtsExtOutPatientVisitInfo> list = getPatientInfo (outPatientVisitInfo);
            if (list == null || list.isEmpty ())
            {
                return null;
            }
            Collections.sort (list, new Comparator <Object> ()
            {
                @Override
                public int compare (Object o1, Object o2)
                {
                    return (int) (((PtsExtOutPatientVisitInfo) o2).getVisitId () - ((PtsExtOutPatientVisitInfo) o1).getVisitId ());
                }
            });
            outPatientVisitInfo = list.get (0);
            // PtsExtOutPatientVisitInfo转化为PatientInfo
            patientInfo.setPatiId (outPatientVisitInfo.getPatientId ());// 患者ID
            patientInfo.setName (outPatientVisitInfo.getPatientName ());// 姓名
            patientInfo.setSex (outPatientVisitInfo.getPatientGenderName ());// 性别
            patientInfo.setBirthDate (outPatientVisitInfo.getBirthdate () == null ? new Date () : outPatientVisitInfo.getBirthdate ());// 出生日期
            patientInfo.setAge (FcAgeUtil.getAgeString (patientInfo.getBirthDate ()));
            patientInfo.setChargeType (outPatientVisitInfo.getChargeTypeName ());// 费别
            patientInfo.setVisitId (outPatientVisitInfo.getVisitId () == null ? 1 : outPatientVisitInfo.getVisitId ());// 就诊次数
            patientInfo.setDeptCode (outPatientVisitInfo.getDeptCode ());
            patientInfo.setDeptName (outPatientVisitInfo.getDeptName ());
            patientInfo.setVisitBeginDate (outPatientVisitInfo.getOutpDate ());
            patientInfo.setHospitalAreaCode (outPatientVisitInfo.getHospitalAreaCode ());
            patientInfo.setHospitalName (outPatientVisitInfo.getHospitalName ());
            queryBalance (patientInfo);
        }
        else
        {
            // 调用住院信息
            PtsExtInPatientVisitInfo pstInfo = null;
            try
            {
                pstInfo = m_ptsExtInPatientVisitQueryService.findSpecifiedInPatientVisitList (patientId).get (0);
            }
            catch (Exception e)
            {
                s_logger.error (e.getLocalizedMessage ());
                throw new BusinessException ("查询住院患者信息失败,联系PTS小组");
            }
            patientInfo.setPatiId (pstInfo.getPatientId ());// 患者ID
            patientInfo.setName (pstInfo.getPatientName ());// 姓名
            patientInfo.setSex (pstInfo.getPatientGenderName ());// 性别
            patientInfo.setBirthDate (pstInfo.getBirthdate () == null ? new Date () : pstInfo.getBirthdate ());// 出生日期
            patientInfo.setAge (FcAgeUtil.getAgeString (patientInfo.getBirthDate ()));
            patientInfo.setChargeType (pstInfo.getCostTypeName ());// 费别
            patientInfo.setVisitId (pstInfo.getVisitId () == null ? 1 : pstInfo.getVisitId ());// 就诊次数
            patientInfo.setDeptName (pstInfo.getCurrentDeptName ());// 在院科室
            patientInfo.setVisitBeginDate (pstInfo.getNewInDeptDate () == null ? new Date () : pstInfo.getNewInDeptDate ()); // 入科时间
            // patientInfo.setVisitDay ();//在科天数
            patientInfo.setBedCode (pstInfo.getBedCode ());// 床号
            patientInfo.setHospitalAreaCode (outPatientVisitInfo.getHospitalAreaCode ());
            patientInfo.setHospitalName (outPatientVisitInfo.getHospitalName ());
            queryBalance (patientInfo);
        }
        return patientInfo;

    }

    private void queryBalance (PatientInfo patientInfo)
    {
        // 根据PatientId得到得到余额

    }

    /**
     * 判断处方来源
     * 
     * @param prescSource
     * @return add by Zli
     */
    private boolean isHospital (Integer prescSource)
    {
        return prescSource.intValue () == PrescSource.DischargeMedication.getEnumCode () || prescSource.intValue () == PrescSource.InHospital.getEnumCode ()
               || prescSource.intValue () == PrescSource.InHospitalDurgReturn.getEnumCode ();
    }

    /**
     * 得到患者信息
     * 
     * @param outPatientVisitInfo
     * @return add by Zli
     */
    private List <PtsExtOutPatientVisitInfo> getPatientInfo (PtsExtOutPatientVisitInfo outPatientVisitInfo)
    {
        List <PtsExtOutPatientVisitInfo> list;
        try
        {
            list = m_ptsExtOutPatientVisitService.findOutPatientVisitInfoList (outPatientVisitInfo);
        }
        catch (Exception e)
        {
            throw new BusinessException (e.getMessage (), e);
        }
        return list;
    }
}
